package com.googlecode.gwtbb.server.intermediate.search.forum;

import com.googlecode.gwtbb.client.shared.entity.forum.Post;
import com.googlecode.gwtbb.server.dao.forum.PostDao;
import com.googlecode.gwtbb.server.intermediate.search.GetIdIndexer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 29, 2008
 */
public class PostIndexer extends GetIdIndexer<Post> {
	private PostDao postDao;

	public PostDao getPostDao() {
		return postDao;
	}

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	protected void addAllDocuments() {
		postDao.handleAll(getAddIndexHandler());
	}

	protected Document createDocument(Post post) {
		Document doc = new Document();
		doc.add(new Field(Post.ID, post.getId().toString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
		doc.add(new Field(Post.THREAD_ID, post.getThreadId().toString(), Field.Store.YES, Field.Index.NO));
		doc.add(new Field(Post.FORUM_ID, post.getForumId().toString(), Field.Store.YES, Field.Index.NO));
		doc.add(new Field(Post.USER_ID, post.getUserId().toString(), Field.Store.YES, Field.Index.NO));
		if (post.isThread())
			doc.add(new Field(Post.SUBJECT, post.getSubject(), Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field(Post.BODY, post.getBody(), Field.Store.YES, Field.Index.TOKENIZED));
		doc.add(new Field(Post.SNAP_DATE, String.valueOf(post.getSnapDate().getTime()), Field.Store.YES, Field.Index.NO));
		doc.add(new Field(Post.CREATE_DATE, String.valueOf(post.getCreateDate().getTime()), Field.Store.YES, Field.Index.NO));
		return doc;
	}
}
